import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { GameComponentType } from "@sapi-game/gameComponent/gameComponent";
import { WereWolfModule } from "@/werewolf/module";

export class WereWolfGapPhase extends WereWolfModule.State<{
    gapTime: number;
    components?: GameComponentType<any>[];
}> {
    override onEnter(): void {
        if (!this.config) return;
        if (this.config.gapTime <= 0) {
            this.next();
            return;
        }
        this.addComponent(Timer, {
            initialTime: this.config.gapTime,
        });
        if (this.config.components) {
            this.addComponents(this.config.components);
        }

        const timer = this.getComponent(Timer);
        this.subscribe(
            timer.events.onTime,
            () => {
                this.next();
            },
            { time: 0 }
        );
        timer.start();
    }

    next() {
        this.popState();
    }
}
